﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace ENTITY_FRAMEWORK
{
    class Program
    {
        static void Main(string[] args)
        {
            //InsertCustomer();
            //UpdateCustomer();
            //DeleteCustomer();
            //ShowAllCustomers();
            //ShowAllLondonCustomers();
            DisplayCustomeOrder();
            
        }

        public static void InsertCustomer()
        {
            Entities2 db = new Entities2();
            
            var customer = new Customers() 
                { CustomerID = "CHIPS", CompanyName = "Mr. Chips" };
            
            //1table.Mtable as field
            //db.Customers.InsertOnSubmit(customer);
            //customer.Orders;
            db.AddToCustomers(customer);
            
            //db.AddObject(
            db.SaveChanges();
        }

        public static void UpdateCustomer()
        {
            Entities2 db = new Entities2();
            
            var only = db.Customers.SingleOrDefault(c => c.CustomerID == "CHIPS");
            //only.CompanyName = only.CompanyName + "1";

            db.SaveChanges();
        }

        public static void DeleteCustomer()
        {
            Entities2 db = new Entities2();

            var first = db.Customers.FirstOrDefault(c => c.CustomerID == "CHIPS");
            //EF don't support: db.Customers.SingleOrDefault
            //var first = db.Customers.SingleOrDefault(c => c.CustomerID == "CHIPS");
            //db.Customers.DeleteOnSubmit(only);
            db.DeleteObject(first);

            db.SaveChanges();
        }

        public static void ShowCustomersByCity()
        {
            Entities2 db = new Entities2();

            var customers = from c in db.Customers
                            where c.City == "London"
                            select c;

            foreach (Customers c in customers)
            {

                Console.WriteLine(c.CompanyName);
            }
        }

        public static void ShowAllCustomers()
        {
            Entities2 db = new Entities2();

            var customers = db.Customers;
            //var customers = from c in db.Customers
            //                select c;

            foreach (Customers c in customers)
            {

                Console.WriteLine(c.CustomerID);
            }
        }

        public static void DisplayCustomeOrder()
        {

            using (Entities2 nw = new Entities2())
            {

                var q = from cus in nw.Customers

                        where cus.City == "London"

                        select cus;

                // Loop through customers and print out orders since January 1, 1998

                foreach (Customers customer in q)
                {

                    Console.WriteLine(customer.CompanyName + ": ");

                    // Note line below to explicitly load all orders for each customer

                    customer.Orders.Load();

                    //Navigating to associated information that has not yet been retrieved from the database will not cause an additional database trip, but rather will only return the information if it was explicitly requested with the first query or with a new query, as seen in Listing 8.
                    foreach (Orders order in customer.Orders.Where(o => o.OrderDate > new DateTime(1998, 1, 1)))
                    {

                        Console.WriteLine("\t{0},{1}", order.OrderID, order.OrderDate);

                    }

                }

                Console.ReadLine();

            }
        }
        
    }

    //public partial class Customers
    //{

    //    partial void OnPhoneChanging(string value)
    //    {

    //        Regex phoneNum = new Regex(@"^[2-9]\d{2}-\d{3}-\d{4}$");

    //        if (phoneNum.IsMatch(value) == false)

    //            throw new Exception("Please enter a valid Phone Number");

    //    }

    //}
}
